package zdh.jfinabbsapp.widget;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.kymjs.kjframe.http.HttpCallBack;

import us.feras.mdv.MarkdownView;
import zdh.jfinabbsapp.R;
import zdh.jfinabbsapp.api.API;
import zdh.jfinabbsapp.api.dao.Topic;
import zdh.jfinabbsapp.util.Session;

/**
 * 作者：zdh1000
 * 创建时间： 2015/7/1 20:16
 * 邮箱：wdzdh1000@163.com
 */
public class TopicFragment extends Fragment {
    private String id;
    private Topic topic;
    private TextView tv_title;
    private TextView tv_author;
    private TextView tv_time;
    private MarkdownView mdView;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            tv_title.setText(topic.getTitle());
            tv_author.setText(topic.getNickName());
            tv_time.setText(topic.getInTime());
            mdView.loadMarkdown(topic.getContent());
        }
    };


    public TopicFragment() {
    }

    public static TopicFragment instance(Bundle bundle) {
        TopicFragment fragment = new TopicFragment();
        fragment.id = bundle.getString("id");
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle saveInstanceState) {
        View view = inflater.inflate(R.layout.fragment_topic, null);
        tv_title = (TextView) view.findViewById(R.id.fragment_topic_textView_title);
        tv_author = (TextView) view.findViewById(R.id.fragment_topic_textView_author);
        tv_time = (TextView) view.findViewById(R.id.fragment_topic_textView_time);
        mdView = (MarkdownView) view.findViewById(R.id.fragment_topic_mdView);
        new NThread().start();
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private class NThread extends Thread {
        @Override
        public void run() {
            String url = API.getTopicUrl(id);
            if (API.checkCache(url) && !Session.isAutoUpdate()) {
                Log.d("NThread", "load topic from cache.");
                String t = new String(API.http.getCache(url));
                topic = API.getTopicByJson(t);
                handler.sendEmptyMessage(0);
            } else {
                Log.d("NThread", "load topic from network.");
                //线程获取栏目，并初始化leftItem
                API.http.get(url, new HttpCallBack() {
                    public void onSuccess(String t) {
                        super.onSuccess(t);
                        Log.d("NThread", t);
                        topic = API.getTopicByJson(t);
                        handler.sendEmptyMessage(0);

                    }
                });
//            }
//
            }
        }

    }
}
